#import "MainViewControler.h"
#import "BootUIView.h"
#import "UIListView.h"
#import "ListItem.h"
#import "ImageConfig.h"
#import "ListItem.h"
#import "DictionaryUtil.h"
#import "FormUIView.h"
#import "ProvinceUIView.h"
#import "CityUIView.h"
#import "ZoneUIView.h"
#import "ResultUIView.h"
#import "HttpRequestParamEnginner.h"

@implementation CMainViewControler

@synthesize iViewClassName;
@synthesize iViewType;
@synthesize iLucktyType;
@synthesize iRect;
@synthesize iWindow;
@synthesize iProvinceId;
@synthesize iCityId;
@synthesize iZoneId;
@synthesize iAllUrl;


-init
{
	if(self=[super init])
	{
		iHttpRequestParam=nil;
		return self;
	}
	return nil;
}


- (void)loadView 
{
	//[super loadView];
	switch (iViewType)
	{
		case BootViewType:
		{
			CBootUIView *bootView=[[CBootUIView alloc]initWithFrame:iRect];
			self.view=bootView;
			[bootView release];
			bootView.backgroundColor=[self GetImgColorFromFile:BOOT_BACKGROUP_IMG_NAME];
			break;
		}
		case MainViewType:
		{
			[iHttpRequestParam release];
			iHttpRequestParam=nil;
			UIListView *listView=[[UIListView alloc]initWithFrame:iRect];
			//self.view.backgroundColor=[self GetImgColorFromFile:BOOT_BACKGROUP_IMG_NAME];
			listView.iType=KListView;
			listView.delegate=self;
			listView.dataSource=self;
			ListItem *item=[[ListItem alloc] init];
			item.iTitle=@"本周运势查询";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"本月运势查询";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"个人性格分析";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"命运分析-事业篇";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"命运分析-家庭篇";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"命运分析-爱情篇";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"命运分析-财运篇";
			[listView addItme:item];
			item=[[ListItem alloc] init];
			item.iTitle=@"命运分析-婚姻篇";
			[listView addItme:item];
			self.view=listView;
			[listView release];
			self.title=@"天缘网-iphone版";
					break;
		}
		case FormViewType:
		{
			CFormUIView *formView=[[CFormUIView alloc]initWithFrame:iRect];
			formView.iViewType=iViewType;
			formView.iLucktyType=iLucktyType;
			self.view=formView;
			[formView release];
			formView.backgroundColor=[self GetImgColorFromFile:MAIN_BACKGROUP_IMG_NAME];
			UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]
													initWithTitle:@"返回"
													style:UIBarButtonItemStyleBordered
													target:self
													action:@selector(callBack)];
			switch (iLucktyType) {
				case LucktyTypeWeek:
					self.title=@"本周运势查询";
					break;
				case LucktyTypeMonth:
					self.title=@"本月运势查询";
					break;
				case LucktyTypeTag:
					self.title=@"个人性格分析";
					break;
				case LucktyTypeFateSY:
					self.title=@"命运分析-事业篇";
					break;
				case LucktyTypeFateJT:
					self.title=@"命运分析-家庭篇";
					break;
				case LucktyTypeFateAQ:
					self.title=@"命运分析-爱情篇";
					break;
				case LucktyTypeFateCY:
					self.title=@"命运分析-财运篇";
					break;
				case LucktyTypeFateHY:
					self.title=@"命运分析-婚姻篇";
					break;
				default:
					break;
			}
			self.navigationItem.leftBarButtonItem=callModalViewButton;
			[callModalViewButton release];

			//self.view.backgroundColor=[self GetImgColorFromFile:BOOT_BACKGROUP_IMG_NAME];
			break;
		}
		case ProvinceViewType:
		{
			
			CProvinceUIView *provinceView=[[CProvinceUIView alloc]initWithFrame:iRect];
			provinceView.iViewType=iViewType;
			provinceView.iLucktyType=iLucktyType;
			provinceView.iDataSource=self;
			provinceView.iDelegate=self;
			self.view=provinceView;
			[provinceView release];
			UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]
													initWithTitle:@"返回"
													style:UIBarButtonItemStyleBordered
													target:self
													action:@selector(callBack)];
			self.title=@"省份选择";
			self.navigationItem.leftBarButtonItem=callModalViewButton;
			[callModalViewButton release];
			break;
		}
		case CityViewType:
		{
			
			CCityUIView *cityView=[[CCityUIView alloc]initWithFrame:iRect];
			cityView.iViewType=iViewType;
			cityView.iLucktyType=iLucktyType;
			cityView.iDataSource=self;
			cityView.iDelegate=self;
			self.view=cityView;
			[cityView release];
			UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]
													initWithTitle:@"返回"
													style:UIBarButtonItemStyleBordered
													target:self
													action:@selector(callBack)];
			self.title=@"城市选择";
			self.navigationItem.leftBarButtonItem=callModalViewButton;
			[callModalViewButton release];
			break;
		}
		case ZoneViewType:
		{
			
			CZoneUIView *zoneView=[[CZoneUIView alloc]initWithFrame:iRect];
			zoneView.iViewType=iViewType;
			zoneView.iLucktyType=iLucktyType;
			zoneView.iDataSource=self;
			zoneView.iDelegate=self;
			self.view=zoneView;
			[zoneView release];
			UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]
													initWithTitle:@"返回"
													style:UIBarButtonItemStyleBordered
													target:self
													action:@selector(callBack)];
			self.title=@"区县选择";
			self.navigationItem.leftBarButtonItem=callModalViewButton;
			[callModalViewButton release];
			break;
		}
		case ResultViewType:
		{
			
			CResultUIView *resultView=[[CResultUIView alloc]initWithFrame:iRect];
			resultView.iViewType=iViewType;
			resultView.iLucktyType=iLucktyType;
			resultView.iAllUrl=iAllUrl;
			self.view=resultView;
			[resultView requestHttp];
			resultView.backgroundColor=[self GetImgColorFromFile:MAIN_BACKGROUP_IMG_NAME];
			[resultView release];
			UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc]
													initWithTitle:@"返回"
													style:UIBarButtonItemStyleBordered
													target:self
													action:@selector(callBack)];
			switch (iLucktyType) {
				case LucktyTypeWeek:
					self.title=@"本周运势查询";
					break;
				case LucktyTypeMonth:
					self.title=@"本月运势查询";
					break;
				case LucktyTypeTag:
					self.title=@"个人性格分析";
					break;
				case LucktyTypeFateSY:
					self.title=@"命运分析-事业篇";
					break;
				case LucktyTypeFateJT:
					self.title=@"命运分析-家庭篇";
					break;
				case LucktyTypeFateAQ:
					self.title=@"命运分析-爱情篇";
					break;
				case LucktyTypeFateCY:
					self.title=@"命运分析-财运篇";
					break;
				case LucktyTypeFateHY:
					self.title=@"命运分析-婚姻篇";
					break;
				default:
					break;
			}
			
			self.navigationItem.leftBarButtonItem=callModalViewButton;
			[callModalViewButton release];
			break;
		}
			
			
	}
}

-(void)callBack
{
	[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationBackView object:self userInfo:nil];
}
- (void)viewDidUnload
{
	int a=0;
	a=1;
}

-(UIColor *)GetImgColorFromFile:(NSString *)imgName
{
	NSString *imgPath=[ [NSBundle mainBundle] pathForResource:imgName ofType:nil];
	UIImage *backGroupImg=[UIImage imageWithContentsOfFile:imgPath];
	return [UIColor colorWithPatternImage:backGroupImg];
}
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	int type = [(UIListView *)tableView iType];
	int rows = 0;
	switch (type) 
	{
		case KListView:
		{
			rows = [[(UIListView *)tableView iItemsArray] count];
			break;
		}
		
	}
	return rows;
	
}	
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = NULL;
    NSUInteger row = [indexPath row];
    
    // Configure the cell...
	int type = [(UIListView *)tableView iType];
	switch (type) {
		case KListView:
		{
			cell =[(UIListView *)tableView dequeueReusableCellWithIdentifier:CellIdentifier];
			if (cell == nil) {
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
				//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
				if(iViewType!=ZoneViewType)
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			}
			
			ListItem* item = [[(UIListView *)tableView iItemsArray] objectAtIndex:row];
						cell.textLabel.text = item.iTitle;
			cell.detailTextLabel.text = item.iSubTitle;
			if(iViewType==MainViewType)
			{
				cell.textLabel.textColor=[UIColor whiteColor];
				
				tableView.backgroundColor=[self GetImgColorFromFile:MAIN_BACKGROUP_IMG_NAME];
			}
			//tableView.backgroundColor=[UIColor colorWithRed:219.0f/255 green:112.0f/255 blue:147.0f/255 alpha:1];
			
				if (item.iFlagImageName) {
				UIImage* img = [[CImageFactory getInstance] getUIImage:item.iFlagImageName];
				cell.imageView.image = img;
				[img release];
			}
			break;
		}
		
	}
    return cell;
	
}

#pragma mark Table view delegate Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return [(UIListView*)tableView getItmeHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	int type = [(UIListView *)tableView iType];
	NSUInteger row = [indexPath row];
	NSMutableDictionary *userInfo;
	userInfo=nil;
	switch (type) {
		case KListView:
		{
			ListItem* item = [[(UIListView *)tableView iItemsArray] objectAtIndex:row];
			switch (iViewType) 
			{
				case MainViewType:
				{
					userInfo=[[NSMutableDictionary alloc] init];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"viewType" tagIntValue:(int)FormViewType];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"lucktyType" tagIntValue:(int)row];
					[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationSwitchView object:self userInfo:userInfo];
					break;
				}
				case ProvinceViewType:
				{
					userInfo=[[NSMutableDictionary alloc] init];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"viewType" tagIntValue:(int)CityViewType];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"lucktyType" tagIntValue:iLucktyType];
					CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
					enginner->iTempParam.iProvinceId=item.iSubTitle;
					enginner->iTempParam.iProvince=item.iTitle;
					[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationSwitchView object:self userInfo:userInfo];
					break;
				}
				case CityViewType:
				{
					userInfo=[[NSMutableDictionary alloc] init];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"viewType" tagIntValue:(int)ZoneViewType];
					[CDictionaryUtil SetIntValue:userInfo tagKey:@"lucktyType" tagIntValue:iLucktyType];
					CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
					enginner->iTempParam.iCityId=item.iSubTitle;
					enginner->iTempParam.iCity=item.iTitle;
					[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationSwitchView object:self userInfo:userInfo];
					break;
				}
				case ZoneViewType:
				{
					CHttpRequestParamEnginner *enginner=[CHttpRequestParamEnginner GetIntance];
					enginner->iParam.iProvinceId=enginner->iTempParam.iProvinceId;
					enginner->iParam.iProvince=enginner->iTempParam.iProvince;
					enginner->iParam.iCityId=enginner->iTempParam.iCityId;
					enginner->iParam.iCity=enginner->iTempParam.iCity;
					enginner->iParam.iZoneId=item.iSubTitle;
					enginner->iParam.iZone=item.iTitle;
					[[NSNotificationCenter defaultCenter] postNotificationName:KNotificationBackFormView object:self userInfo:nil];
					break;
				}
					
				
				default:
					break;
			}
			break;
		}
		
	}
	[userInfo release];
}
-(void)UpdateView
{
	switch (iViewType)
	{
		case FormViewType:
		{
			CFormUIView *view=(CFormUIView*)(self.view);
			[view UpdateView];
			break;
		}
	}
}

-(void)dealloc
{
	if(iAllUrl)
	{
		[iAllUrl release];
	}
	[super dealloc];
}
@end
